package com.fc.mall.service.impl;/**
 * Created by Administrator on 2019/10/13.
 */

import com.fc.mall.dto.SmsFriendsCirclesQueryParam;
import com.fc.mall.mapper.SmsFriendsCirclesMapper;
import com.fc.mall.model.SmsFriendsCircles;
import com.fc.mall.model.SmsFriendsCirclesExample;
import com.fc.mall.service.SmsFriendsCirclesService;
import com.fc.mall.util.DateTimeHelper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author Climb
 * @Date 2019/10/13
 **/
@Service
public class SmsFriendsCirclesServiceImpl implements SmsFriendsCirclesService {
    @Autowired
    private SmsFriendsCirclesMapper smsFriendsCirclesMapper;

    /**
     * 获取列表
     * @param queryParam
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<SmsFriendsCircles> getList(SmsFriendsCirclesQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SmsFriendsCirclesExample example = new SmsFriendsCirclesExample();
        SmsFriendsCirclesExample.Criteria criteria= example.createCriteria();
        if (queryParam.getTitle() != null){
            criteria.andTitleLike("%"+queryParam.getTitle()+"%");
        }
        if (queryParam.getStatus() != null){
            criteria.andStatusEqualTo(queryParam.getStatus());
        }
        if (queryParam.getStartCreateTime() != null && queryParam.getEndCreateTime() != null){
            Date startCreateTime = DateTimeHelper.getDateFromString(queryParam.getStartCreateTime(),DateTimeHelper.SQL_DATETIME);
            Date endCreateTime = DateTimeHelper.getDateFromString(queryParam.getEndCreateTime(),DateTimeHelper.SQL_DATETIME);
            criteria.andCreateTimeBetween(startCreateTime,endCreateTime);
        }
        List<SmsFriendsCircles> list = smsFriendsCirclesMapper.selectByExample(example);
        list.forEach(smsFriendsCircles -> {
            List<String> picList = new ArrayList<>();
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic1()))
            {
                picList.add(smsFriendsCircles.getPic1());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic2()))
            {
                picList.add(smsFriendsCircles.getPic2());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic3()))
            {
                picList.add(smsFriendsCircles.getPic3());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic4()))
            {
                picList.add(smsFriendsCircles.getPic4());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic5()))
            {
                picList.add(smsFriendsCircles.getPic5());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic6()))
            {
                picList.add(smsFriendsCircles.getPic6());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic7()))
            {
                picList.add(smsFriendsCircles.getPic7());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic8()))
            {
                picList.add(smsFriendsCircles.getPic8());
            }
            if(!StringUtils.isEmpty(smsFriendsCircles.getPic9()))
            {
                picList.add(smsFriendsCircles.getPic9());
            }
            smsFriendsCircles.setPicList(picList);
        });
        return list;
    }

    /**
     * 通过Id获取好友朋友圈
     * @param id
     * @return
     */
    @Override
    public SmsFriendsCircles getById(Long id) {
        SmsFriendsCircles smsFriendsCircles = smsFriendsCirclesMapper.selectByPrimaryKey(id);
        List<String> picList = new ArrayList<>();
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic1()))
        {
            picList.add(smsFriendsCircles.getPic1());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic2()))
        {
            picList.add(smsFriendsCircles.getPic2());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic3()))
        {
            picList.add(smsFriendsCircles.getPic3());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic4()))
        {
            picList.add(smsFriendsCircles.getPic4());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic5()))
        {
            picList.add(smsFriendsCircles.getPic5());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic6()))
        {
            picList.add(smsFriendsCircles.getPic6());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic7()))
        {
            picList.add(smsFriendsCircles.getPic7());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic8()))
        {
            picList.add(smsFriendsCircles.getPic8());
        }
        if(!StringUtils.isEmpty(smsFriendsCircles.getPic9()))
        {
            picList.add(smsFriendsCircles.getPic9());
        }
        smsFriendsCircles.setPicList(picList);
        return smsFriendsCircles;
    }

    /**
     * 增加，修改API
     * @param friendsCircles
     * @return
     */
    @Override
    public int createOrUpdate(SmsFriendsCircles friendsCircles) {
        if (null!=friendsCircles.getPicList() && !friendsCircles.getPicList().isEmpty()){
            int i = 1;
            for(String url: friendsCircles.getPicList())
            {
                if(i == 1)
                {
                    friendsCircles.setPic1(url);
                }

                if(i == 2)
                {
                    friendsCircles.setPic2(url);
                }

                if(i == 3)
                {
                    friendsCircles.setPic3(url);
                }

                if(i == 4)
                {
                    friendsCircles.setPic4(url);
                }

                if(i == 5)
                {
                    friendsCircles.setPic5(url);
                }

                if(i == 6)
                {
                    friendsCircles.setPic6(url);
                }

                if(i == 7)
                {
                    friendsCircles.setPic7(url);
                }

                if(i == 8)
                {
                    friendsCircles.setPic8(url);
                }

                if(i == 9)
                {
                    friendsCircles.setPic9(url);
                }
                i++;
            }
        }
        if (null == friendsCircles.getId()){
            //插入时设置状态为：已发布
            friendsCircles.setStatus(1);
            friendsCircles.setCreateTime(new Date());
            smsFriendsCirclesMapper.insertSelective(friendsCircles);
        }else {
            smsFriendsCirclesMapper.updateByPrimaryKeySelective(friendsCircles);
        }
        return 0;
    }

    /**
     * 通过ID删除
     * @param id
     * @return
     */
    @Override
    public int delById(Long id) {
        return smsFriendsCirclesMapper.deleteByPrimaryKey(id);
    }
}
